package com.cheng;

import com.cheng.entity.Address;
import com.cheng.entity.SClass;
import com.cheng.entity.Student;
import com.cheng.mapper.StudentMapper;
import jdk.nashorn.internal.runtime.regexp.joni.encoding.CharacterType;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class test {
    @Test
    public void test1() throws IOException {
        Reader reader = Resources.getResourceAsReader("xml/conf.xml");
        SqlSessionFactory localhost = new SqlSessionFactoryBuilder().build(reader, "localhost");//localhost指定数据库连接信息
        SqlSession sqlSession = localhost.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.queryAllStudentBySno(1);
        System.out.println(student);
        sqlSession.close();
    }
    @Test
    public void test2() throws IOException {
        Reader reader = Resources.getResourceAsReader("xml/conf.xml");
        SqlSessionFactory localhost = new SqlSessionFactoryBuilder().build(reader/*, "localhost"*/);//localhost指定数据库连接信息
        SqlSession sqlSession = localhost.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = new Student();
        Address address = new Address();
        address.setsAddress("s");
        address.setfAddress("v");
        student.setAddress(address);
        System.out.println(student);
        List<Student> student1 = mapper.queryStudentByAddress(student);
        System.out.println(student1);
        sqlSession.close();
    }

    @Test
    public void test3() throws IOException {
        Reader reader = Resources.getResourceAsReader("xml/conf.xml");
        SqlSessionFactory localhost = new SqlSessionFactoryBuilder().build(reader/*, "localhost"*/);//localhost指定数据库连接信息
        SqlSession sqlSession = localhost.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Map<String,Object> map=new HashMap<>();
        map.put("sAddress","s");
        map.put("fAddress","v");
        List<Student> student1 = mapper.queryStudentByAddressHashMap(map);
        System.out.println(student1);
        sqlSession.close();
    }
    @Test
    public void test4() throws IOException {
        Reader reader = Resources.getResourceAsReader("xml/conf.xml");
        SqlSessionFactory localhost = new SqlSessionFactoryBuilder().build(reader/*, "localhost"*/);//localhost指定数据库连接信息
        SqlSession sqlSession = localhost.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Map<String,Object> map=new HashMap<>();
        map.put("name","李金城");
        mapper.queryStudentBySnoInCunC(map);//调用存储过程
        //处理返回值
        int count =(int)map.get("count");
        System.out.println(count);
        sqlSession.close();
    }
    @Test
    public void test5() throws IOException {
        Reader reader = Resources.getResourceAsReader("xml/conf.xml");
        SqlSessionFactory localhost = new SqlSessionFactoryBuilder().build(reader, "localhost");//localhost指定数据库连接信息
        SqlSession sqlSession = localhost.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<HashMap<String,Object>> studentMap = mapper.queryStudentResultTypeISHashMap();
        System.out.println(studentMap);
        sqlSession.close();
    }
    @Test
    public void test6() throws IOException {
        Reader reader = Resources.getResourceAsReader("xml/conf.xml");
        SqlSessionFactory localhost = new SqlSessionFactoryBuilder().build(reader, "localhost");//localhost指定数据库连接信息
        SqlSession sqlSession = localhost.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student=new Student();
        student.setSname("李金城");
        student.setSphone("15521509548");
        List<Student> student1 = mapper.queryStudentBySQLDynamic(student);
        System.out.println(student1);
        sqlSession.close();
    }
    @Test
    public void test7() throws IOException {
        Reader reader = Resources.getResourceAsReader("xml/conf.xml");
        SqlSessionFactory localhost = new SqlSessionFactoryBuilder().build(reader, "localhost");//localhost指定数据库连接信息
        SqlSession sqlSession = localhost.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        SClass sClass=new SClass();
        List<Integer> intList=new ArrayList<>();
        intList.add(1);
        intList.add(2);
        intList.add(5);
        sClass.setSno(intList);
        List<Student> student1 = mapper.queryStudentBySQLForeach(sClass);
        System.out.println(student1);
        sqlSession.close();
    }
    @Test
    public void MyBatisGenerator() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
        //逆向工程通过student表生成另外三个
        File file = new File("src/generator.xml") ;//配置文件

        List<String> warnings = new ArrayList<>();
        ConfigurationParser cp = new ConfigurationParser(warnings);

        Configuration config = cp.parseConfiguration(file);


        DefaultShellCallback callBack = new DefaultShellCallback(true);

        //逆向工程的核心类
        MyBatisGenerator generator = new MyBatisGenerator(config, callBack,warnings  );
        generator.generate(null);
    }
}